﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using TomanuExtensions;
using TomanuExtensions.Utils;
using AmiEmuLib.Host.HostSound;
using System.Diagnostics;

namespace AmiEmuLib.AmigaConfiguration.AudioConfig
{
    /// <summary>
    /// Sound configuration.
    /// </summary>
    public class Audio : XmlBase
    {
        /// <summary>
        /// Generate sound. Releated to host and amiga. We do not process sounds at all.
        /// </summary>
        public bool Enabled;

        /// <summary>
        /// Sound delay in ms. In fact this is host sound buffer length. If too high we will see that we jump and 
        /// sound comes later. If too low sound may shutter.
        /// </summary>
        public int Latency = 100;

        /// <summary>
        /// Should be between 0..100
        /// </summary>
        public int Volume = 100;

        /// <summary>
        /// Sound filter. Related to amiga hardware filter.
        /// </summary>
        public AmigaAudioFilter Filter = AmigaAudioFilter.EmulatedA500;

        /// <summary>
        /// Host audio sampling rate.
        /// </summary>
        public HostAudioSamplingRate SamplingRate = HostAudioSamplingRate.FQ_44100;

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="a_reader"></param>
        public Audio(XmlReader a_reader)
            : base(a_reader)
        {
        }

        /// <summary>
        /// Get default amiga filter type for specified amiga model.
        /// </summary>
        /// <param name="a_model"></param>
        /// <returns></returns>
        public static AmigaAudioFilter GetDefaultFilter(AmigaModel a_model)
        {
            switch (a_model)
            {
                case AmigaModel.CDTV:
                case AmigaModel.A500:
                case AmigaModel.A500_Plus:
                case AmigaModel.A600:
                case AmigaModel.A2000:
                case AmigaModel.A3000:
                {
                    return AmigaAudioFilter.EmulatedA500;
                }
                case AmigaModel.A1000:
                {
                    return AmigaAudioFilter.AlwaysOnA500;
                }
                case AmigaModel.CD32:
                case AmigaModel.A1200:
                case AmigaModel.A4000:
                {
                    return AmigaAudioFilter.EmulatedA1200;
                }
                case AmigaModel.Custom:
                {
                    return AmigaAudioFilter.EmulatedA500;
                }
                default:
                {
                    Debug.Fail("");
                    return AmigaAudioFilter.EmulatedA500;
                }
            }
        }

        protected override void ReadXml(XmlReader a_reader)
        {
            Enabled = a_reader.GetAttributeBool("Enabled");

            a_reader.ReadStartElement(GetType().Name);

            Latency = a_reader.ReadElementContentAsInt("Latency");
            Volume = a_reader.ReadElementContentAsInt("Volume");
            Filter = a_reader.ReadElementContentAsEnum<AmigaAudioFilter>("Filter");
            SamplingRate = (HostAudioSamplingRate)Enum.ToObject(typeof(HostAudioSamplingRate),
                a_reader.ReadElementContentAsInt("SamplingRate"));

            a_reader.ReadEndElement();
        }

        public override void WriteXml(XmlWriter a_writer)
        {
            a_writer.WriteStartElement(GetType().Name);

            a_writer.WriteAttribute("Enabled", Enabled);
            a_writer.WriteElement("Latency", Latency);
            a_writer.WriteElement("Volume", Volume);
            a_writer.WriteElement("Filter", Filter);
            a_writer.WriteElement("SamplingRate", (int)SamplingRate);

            a_writer.WriteEndElement();
        }
    }
}
